package game.jabberclient;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Opens the right file for questions. Randomly selects a question and aswers.
 * 
 * @author Mina
 * 
 */
public class Question {

	protected FileInputStream fstream = null;
	protected String question;
	protected String[] options = new String[4];
	protected int n;

	public String getQuestion() {
		return question;
	}

	public Object[] getOptions() {
		return options;
	}

	public int getRightAnswer() {
		return n;
	}

	public Question(String category) {
		List<String> lines = new ArrayList<String>();
		try {
			if (category.equals("Art")) {
				fstream = new FileInputStream("res/Art.txt");
			} else if (category.equals("Geography")) {
				fstream = new FileInputStream("res/Geography.txt");
			} else if (category.equals("History")) {
				fstream = new FileInputStream("res/History.txt");
			} else if (category.equals("Science")) {
				fstream = new FileInputStream("res/Science.txt");
			} else if (category.equals("Sport")) {
				fstream = new FileInputStream("res/Sport.txt");
			} else if (category.equals("Varia")) {
				fstream = new FileInputStream("res/Varia.txt");
			}
			String line = null;
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			while ((line = br.readLine()) != null) {
				lines.add(line);
				System.out.println(line);
			}
			in.close();
			Random r = new Random();
			String currentQ = lines.get(r.nextInt(lines.size()));
			String[] current = currentQ.split("&&");
			question = current[0];
			options[0] = current[1];
			options[1] = current[2];
			options[2] = current[3];
			options[3] = current[4];
			n = Integer.parseInt(current[5]);
			System.out.println("\n" + n);

		} catch (Exception e) {
			System.err.println("Error: ... " + e.getMessage());
		}

	}
}